package com.example.getfromservice.Book;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.util.Xml;

public class DataHandlerBook 
{
	String ns = "";
	String connectionString =  "http://10.0.2.2:8020/categoryservice";

	public DataHandlerBook()
	{

	}

	private String getDataService(String urla)
	{
		URL url;
		String line = "", data = "";
		try
		{
			url = new URL(urla);

			HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();

			BufferedReader reader = null;
			reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

			while ((line = reader.readLine()) != null)
			{
				data += line;
			}
			urlConn.disconnect();
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return data;
	}

	

	private Book readBook(XmlPullParser xml, String categoryId)
	{
		String id = "";
		String isbn = "";
		String title = "";
		String author = "";
		String image = null;
		try
		{
			xml.require(XmlPullParser.START_TAG, ns, "books");
			while (xml.next() != XmlPullParser.END_TAG)
			{
				if (xml.getEventType() != XmlPullParser.START_TAG)
					continue;
				if (xml.getName().equals("bookId"))
				{
					xml.require(XmlPullParser.START_TAG, ns, "bookId");
					if (xml.next() == XmlPullParser.TEXT)
					{
						id = xml.getText();
						xml.nextTag();
					}
					xml.require(XmlPullParser.END_TAG, ns, "bookId");
				}

				if (xml.getName().equals("bookISBNnumber"))
				{
					xml.require(XmlPullParser.START_TAG, ns, "bookISBNnumber");
					if (xml.next() == XmlPullParser.TEXT)
					{
						isbn = xml.getText();
						xml.nextTag();
					}
					xml.require(XmlPullParser.END_TAG, ns, "bookISBNnumber");
				}

				if (xml.getName().equals("author"))
				{
					xml.require(XmlPullParser.START_TAG, ns, "author");
					if (xml.next() == XmlPullParser.TEXT)
					{
						author = xml.getText();
						xml.nextTag();
					}
					xml.require(XmlPullParser.END_TAG, ns, "author");
				}

				if (xml.getName().equals("bookName"))
				{
					xml.require(XmlPullParser.START_TAG, ns, "bookName");
					if (xml.next() == XmlPullParser.TEXT)
					{
						title = xml.getText();
						xml.nextTag();
					}
					xml.require(XmlPullParser.END_TAG, ns, "bookName");
				}

				if (xml.getName().equals("image"))
				{
					xml.require(XmlPullParser.START_TAG, ns, "image");
					if (xml.next() == XmlPullParser.TEXT)
					{
						image = xml.getText();
						xml.nextTag();
					}
					xml.require(XmlPullParser.END_TAG, ns, "image");
				}
			}

		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Book b = new Book();
		b.setAuthor(author);
		b.setCategory_id(categoryId);
		b.setID(Integer.parseInt(id));
		b.setISBN(isbn);
		b.setName(title);
		
		byte[] data = Base64.decode(image, Base64.DEFAULT);
		Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
		b.setImage(bitmap);

		return b;
	}

	public List<Book> getAllBooks(String idCategory)
	{
		String parseString = getDataService(connectionString + "/category/" + idCategory + "/books");
		List<Book> list = null;
		try
		{
			XmlPullParser xml = Xml.newPullParser();
			xml.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			xml.setInput(new StringReader(parseString));
			xml.nextTag();
			list = getBooksForCategoryId(xml, idCategory);
		} catch (Exception e)
		{
			return null;
		}

		return list;
	}

	private List<Book> getBooksForCategoryId(XmlPullParser xml, String categoryId)
	{

		List<Book> list = new ArrayList<Book>();

		try
		{

			xml.require(XmlPullParser.START_TAG, ns, "Category");
			while (xml.next() != XmlPullParser.END_TAG)
			{
				if (xml.getEventType() != XmlPullParser.START_TAG)
					continue;
				if (xml.getName().equals("books"))
				{
					list.add(readBook(xml, categoryId));
				} else
				{
					if (xml.getEventType() != XmlPullParser.START_TAG)
					{
						throw new IllegalStateException();
					}
					int depth = 1;
					while (depth != 0)
					{
						switch (xml.next())
						{
						case XmlPullParser.END_TAG:
							depth--;
							break;
						case XmlPullParser.START_TAG:
							depth++;
							break;
						}
					}
				}
			}

		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public List<Book> getAllBooks()
	{
		String parseString = getDataService(connectionString + "/books");
		String categoryId = null;

		XmlPullParser xml = Xml.newPullParser();
		List<Book> list = new ArrayList<Book>();

		try
		{
			xml.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
			xml.setInput(new StringReader(parseString));
			xml.nextTag();

			xml.require(XmlPullParser.START_TAG, ns, "Categorys");
			while (xml.next() != XmlPullParser.END_TAG)
			{
				if (xml.getEventType() != XmlPullParser.START_TAG)
					continue;
				if (xml.getAttributeName(0).equals("categoryId"))
				{
					categoryId = xml.getAttributeValue(0);
				}

				if (xml.getName().equals("Category"))
				{
					list.addAll(getBooksForCategoryId(xml, categoryId));

				} else
				{
					if (xml.getEventType() != XmlPullParser.START_TAG)
					{
						throw new IllegalStateException();
					}
					int depth = 1;
					while (depth != 0)
					{
						switch (xml.next())
						{
						case XmlPullParser.END_TAG:
							depth--;
							break;
						case XmlPullParser.START_TAG:
							depth++;
							break;
						}
					}
				}
			}

		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}

}
